﻿using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Collections.Generic;


namespace Scrobbler
{
    internal class CHistory
    {



        internal CHistory(IHistory history)
        {
            TimerCallback callback = new TimerCallback(history.History_Timer_Tick);
            Timer timer = new Timer(callback);
            timer.Change(0, 300000); //Every 5 minutes

            try
            {
                string CachePath;
                
                CachePath = Environment.GetEnvironmentVariable("APPDATA");
                CachePath += @"\Scrobbler\";

                m_CachePath = CachePath + @"Cache";

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }



        private string m_CachePath;
        private bool m_IsCacheLocked = false;
        private List<string> m_Cache;
       

        internal int CachedSongCount
        {
            get { return this.SongCount(); }
        }


        internal bool IsCacheLocked
        {
            get { return m_IsCacheLocked; }
        }



        internal void SubmitToCache(string artist,string track,string lenght)
        {
            StreamWriter writer;

            try
            {
                writer = new StreamWriter(m_CachePath,true);
                m_IsCacheLocked = true;
                writer.WriteLine(artist + ";" + track + ";" + lenght);
                writer.Close();
                m_IsCacheLocked = false;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }



        internal void RemoveSongFromCache(int index)
        {
            StreamWriter writer;
            List<string> TempSongList = this.Cache;

            try
            {
                writer = new StreamWriter(m_CachePath);
                m_IsCacheLocked = true;
                TempSongList.RemoveAt(index);

                foreach (string line in TempSongList)
                    writer.WriteLine();

                writer.Close();
                m_IsCacheLocked = false;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }



        internal List<string> Cache
        {
            get
            {
                if (m_Cache != null)
                    return m_Cache;
                else
                    return new List<string>();
            }//this.GetCache(); }
        }


      
        internal void UpdateCache()
        {
            StreamReader reader;
            string line;
            m_Cache = new List<string>();

            try
            {
                if (m_IsCacheLocked == false)
                {
                    reader = new StreamReader(m_CachePath);
                    m_IsCacheLocked = true;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line != "")
                            m_Cache.Add(line);
                    }

                    reader.Close();
                    m_IsCacheLocked = false;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }



        private int SongCount()
        {
            StreamReader reader;
            string line;
            int counter = 0;

            try
            {
                if (m_Cache != null)
                    return m_Cache.Count;
                else
                {
                    reader = new StreamReader(m_CachePath);
                    m_IsCacheLocked = true;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line != "")
                            counter++;
                    }
                    reader.Close();
                    m_IsCacheLocked = false;

                    return counter;
                }

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
         
        }




    }
}
